/*
 *  Copyright 2010 mazhao.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package shopping.front;

import com.opensymphony.xwork2.ActionContext;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import shopping.commons.Util;
import shopping.model.Product;
import shopping.model.ShoppingCart;

/**
 * @author mazhao
 */
public class ShoppingCartAction extends FrontAction {

    /**
     * log object
     */
    private Log log = LogFactory.getLog(ShoppingCartAction.class);


    /**
     * preparing data for showing in the shopping cart page.
     *
     * @return shopping cart page
     */
    @Action(
            value = "/front/cart",
            results = {
                    @Result(name = "success", location = "/WEB-INF/content/front/cart.jsp")
            }
    )
    public String show() {
        if (log.isDebugEnabled()) {
            log.debug("enter front/cart action");
        }

//        ShoppingCart sc = (ShoppingCart) ActionContext.getContext().getSession().get(ShoppingCart.SK_SHOPPING_CART);
        ShoppingCart sc = Util.Shopping.retriveShoppingCart();
        if (sc != null) {
            Map<Integer, Integer> ids = sc.getItems();
            Iterator<Map.Entry<Integer, Integer>> pds = ids.entrySet().iterator();
            while (pds.hasNext()) {
                Map.Entry<Integer, Integer> pd = pds.next();
                int productId = pd.getKey();
                int account = pd.getValue();

                // load detail from product
                Product product = sc.getProduct(productId);
                this.items.put(product, account);
            }

            if (log.isDebugEnabled()) {
                Iterator<Map.Entry<Product, Integer>> pds2 = this.items.entrySet().iterator();
                while (pds2.hasNext()) {
                    Map.Entry<Product, Integer> it = pds2.next();
                    log.debug("product:" + it.getKey());
                    log.debug("account:" + it.getValue());
                }
            }
            log.debug("go to shopping cart page");
            return SUCCESS;
        } else {
            log.error("while showing shopping cart, there is no item at all.");
            return INPUT;
        }
    }

    // ===============================================
    // action data section
    // ===============================================

    /**
     * information to show in the shopping cart page
     */
    private Map<Product, Integer> items = new HashMap<Product, Integer>();

    /**
     * get all items.
     *
     * @return
     */
    public Map<Product, Integer> getItems() {
        return items;
    }

    /**
     * set all items.
     *
     * @param items
     */
    public void setItems(Map<Product, Integer> items) {
        this.items = items;
    }


}
